package alo.p6.work5;

import alo.p6.BinarySearchTreeNode;

/**
 * @ClassName IsAVL
 * @Description 判断一棵二叉树是否为平衡二叉树
 * @Author Fanxc
 * @DATE 2021/3/8 22:17
 * @Version 1.0
 */
public class IsAVL {
    //判断一个二叉搜索树是否为平衡二叉树的条件
    //是左右孩子的高度差是否>1
    int isAVL(BinarySearchTreeNode root){
        int left,right;
        if (root==null){
            return -1;
        }
        left=isAVL(root.getLeft());
        if (left==-1){
            return left;
        }
        right=isAVL(root.getRight());
        if (right==-1){
            return right;
        }
        if (Math.abs(left-right)>1){
            return -1;
        }
        return Math.max(left,right)+1;
    }
}
